<template>
  <div id="detail1">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav"/>
    <scroll class="content" 
            ref="scroll" 
            :probe-type="3" 
            @scroll="contentScroll"
            >
      <!-- <div>{{$store.state.cartList.length}}</div>
      <ul>
        <li v-for="(item, index) in $store.state.cartList" :key="index">
          {{item}}
        </li>
      </ul> -->
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detail-info="detailInfo" @detailimageLoad="detailimageLoad"/>
      <detail-param-info ref="params" :param-info="paramInfo"/>
      <detail-comment-info ref="comment" :comment-info="commentInfo"/>
      <goods-list ref="recommend" :goods="recommends"/>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"/>

    <detail-bottom-bar @addToCart="addToCart"/>
    <!-- <toast :message="message" :show="show"/> -->
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar"
import DetailSwiper from "./childComps/DetailSwiper"
import DetailBaseInfo from './childComps/DetailBaseInfo.vue'
import DetailShopInfo from './childComps/DetailShopInfo.vue'
import DetailGoodsInfo from './childComps/DetailGoodsInfo.vue'
import DetailParamInfo from './childComps/DetailParamInfo.vue'
import DetailCommentInfo from './childComps/DetailCommentInfo.vue'
import DetailBottomBar from './childComps/DetailBottomBar.vue'

import Scroll from '@/components/common/scroll/Scroll.vue'
import GoodsList from '@/components/content/goods/GoodsList'
// import Toast from '@/components/common/toast/Toast'

import {getDetail, Goods, getRecommend, Shop, GoodsParam} from "@/network/detail"
// import {debounce} from "@/common/utils"
import {itemListenerMixin, backTopMixin} from "@/common/mixin"
import {mapActions} from 'vuex'

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    // Toast
  },
  mixins: [itemListenerMixin, backTopMixin],

  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0,
      // message: '',
      // show: false
    }
  },
  
  created() {
    // 保存传入的iid
    this.iid = this.$route.params.iid
    // console.log('this.iid', this.$route.params);

    // 2.根据iid请求详情数据
    getDetail(this.iid).then(res => {
      console.log('详情页数据',res); //数据太多了  要分离
      const data = res.result;

      // 1.获取顶部图片轮播数据
      this.topImages = data.itemInfo.topImages
      
      // 2.获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

      // 3.创建店铺信息的对象
      this.shop = new Shop(data.shopInfo)

      // 4.保存商品的详细数据
      this.detailInfo = data.detailInfo

      // 5.获取参数信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

      // 6.获取评论信息  :/有些商品没有评论
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0]
      }
      
      // 第一次获取，值不对
      // 原因： this.$refs.params.$el压根没有渲染
      // this.themeTopYs = []

      // this.themeTopYs.push(0)
      // this.themeTopYs.push(this.$refs.params.$el.offsetTop) //参数的offsetTop
      // this.themeTopYs.push(this.$refs.comment.$el.offsetTop) //评论的offsetTop
      // this.themeTopYs.push(this.$refs.recommend.$el.offsetTop) //推荐的offsetTop

      // console.log(this.themeTopYs);

      this.$nextTick(() => {    // 前面的渲染好了，才执行这个
        // 2.第二次获取：值不对
        // 图片没有计算在内
        this.themeTopYs = []

        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.params.$el.offsetTop-44) //参数的offsetTop
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop-44) //评论的offsetTop
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop-44) //推荐的offsetTop
        this.themeTopYs.push(Number.MAX_VALUE)

        // console.log(this.themeTopYs);
      })

    })
  
    // 3.请求推荐数据
    getRecommend().then(res => {
      this.recommends = res.data.list
    })

    // // 4.给getThemeTopY赋值   //在图片加载完成后
    // this.getThemeTopY = debounce(() => {  //外面层没执行 失败
    // //  console.log("给getThemeTopY赋值");
    //   this.themeTopYs = []
    //   this.themeTopYs.push(0)
    //   this.themeTopYs.push(this.$refs.params.$el.offsetTop) //参数的offsetTop
    //   this.themeTopYs.push(this.$refs.comment.$el.offsetTop) //评论的offsetTop
    //   this.themeTopYs.push(this.$refs.recommend.$el.offsetTop) //推荐的offsetTop
    //   // console.log(this.themeTopYs);
    // }, 100)
  },
  destroyed() {
    this.$bus.$off('itemImageLoad', this.itemImgListener)
  },
  methods: {
    ...mapActions(['addCart']),
    detailimageLoad() {
      // console.log(1111111)
      this.refresh()
      // this.$refs.scroll.refresh()
    },
    titleClick(index) {
      // console.log("titleClick",index);
      //滚动到某个位置
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 100)
    },
    contentScroll(position) {
      // 1.获取y值
      const positionY = -position.y

      //2.positionY和主题里面的值进行对比
      for(let i = 0; i < this.themeTopYs.length - 1 ; i++){
          // console.log(i);
          if(this.currentIndex !== i && (positionY > this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])){
              this.currentIndex = i
              this.$refs.nav.currentIndex = i
              // console.log(this.currentIndex);
          }
      }

      // 3.判断BackTop是否显示
      this.isShowBackTop = (-position.y) > 1000
    },
    addToCart() {
      // 1.获取购物车需要展示的信息
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.title
      product.price = this.goods.realPrice
      product.iid = this.iid

      // 2.将商品添加到购物车里面
      // this.$store.cartList.push(product)
      // this.$store.commit('addCart', product)

      this.$store.dispatch('addCart',product).then(res => {
          // this.show = true;
          // this.message = res;
          
          // setTimeout(() => {
          //   this.show = false
          // }, 1550)

          this.$toast.show(res, 2000)
          // console.log('6666666666666666666666666', this.$toast);
        }
      ) 

      // this.addCart(product).then(res => {
      //           this.$toast.show(res,2000)
      //       })
    }
  }
}
</script>

<style scoped>
  #detail1 {
    position: relative;
    z-index: 9;
    background-color: #fff;
    height: 100vh;
  }

  .detail-nav {
    position: relative;
    top: 0;
    z-index: 9;
    background-color: #fff;
  }

  .content {
    height: calc(100% - 93px);
    overflow: hidden;
  }


</style>